package com.guowen.myaccount;


import androidx.test.espresso.DataInteraction;
import androidx.test.espresso.ViewInteraction;
import androidx.test.filters.LargeTest;
import androidx.test.rule.ActivityTestRule;
import androidx.test.runner.AndroidJUnit4;

import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;

import static androidx.test.InstrumentationRegistry.getInstrumentation;
import static androidx.test.espresso.Espresso.onData;
import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.Espresso.pressBack;
import static androidx.test.espresso.Espresso.openActionBarOverflowOrOptionsMenu;
import static androidx.test.espresso.action.ViewActions.*;
import static androidx.test.espresso.assertion.ViewAssertions.*;
import static androidx.test.espresso.matcher.ViewMatchers.*;

import com.guowen.myaccount.R;
import com.guowen.myaccount.db.AccountBean;
import com.guowen.myaccount.db.DBManager;

import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.hamcrest.core.IsInstanceOf;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.anything;
import static org.hamcrest.Matchers.is;

@LargeTest
@RunWith(AndroidJUnit4.class)
public class MainActivityTest {

    @Rule
    public ActivityTestRule<MainActivity> mActivityTestRule = new ActivityTestRule<>(MainActivity.class);

    AccountBean bean = new AccountBean(0, R.mipmap.ic_qita_fs, 0, "其他", "123456", "12:30", "现金", 120, 2021, 12, 12);
    AccountBean bean1 = new AccountBean(0, R.mipmap.ic_qita_fs, 0, "其他", "123456", "12:30", "现金", 120, 2021, 12, 12);
    AccountBean bean2 = new AccountBean(0, R.mipmap.ic_qita_fs, 0, "其他", "123456", "12:30", "现金", 120, 2021, 12, 12);

    @Before
    public void setUp(){
        DBManager.insertItemToAccounttb(bean);
        DBManager.insertItemToAccounttb(bean1);
        DBManager.insertItemToAccounttb(bean2);
    }

    @After
    public void tearBack(){
        DBManager.deleteItemFromAccounttbById(bean.getId());
        DBManager.deleteItemFromAccounttbById(bean1.getId());
        DBManager.deleteItemFromAccounttbById(bean2.getId());
    }

    @Test
    public void mainActivityTest() {
        ViewInteraction appCompatImageView = onView(
                allOf(withId(R.id.main_iv_more),
                        childAtPosition(
                                childAtPosition(
                                        withId(android.R.id.content),
                                        0),
                                2),
                        isDisplayed()));
        appCompatImageView.perform(click());

        ViewInteraction tabView = onView(
                allOf(withContentDescription("收入"),
                        childAtPosition(
                                childAtPosition(
                                        withId(R.id.record_tabs),
                                        0),
                                1),
                        isDisplayed()));
        tabView.perform(click());

        ViewInteraction appCompatImageView2 = onView(
                allOf(withId(R.id.record_iv_back),
                        childAtPosition(
                                childAtPosition(
                                        withClassName(is("android.widget.LinearLayout")),
                                        0),
                                0),
                        isDisplayed()));
        appCompatImageView2.perform(click());
    }

    private static Matcher<View> childAtPosition(
            final Matcher<View> parentMatcher, final int position) {

        return new TypeSafeMatcher<View>() {
            @Override
            public void describeTo(Description description) {
                description.appendText("Child at position " + position + " in parent ");
                parentMatcher.describeTo(description);
            }

            @Override
            public boolean matchesSafely(View view) {
                ViewParent parent = view.getParent();
                return parent instanceof ViewGroup && parentMatcher.matches(parent)
                        && view.equals(((ViewGroup) parent).getChildAt(position));
            }
        };
    }
}
